package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.mapper.GysptTestMapper;
import com.ruoyi.system.domain.GysptTest;
import com.ruoyi.system.service.IGysptTestService;
import com.ruoyi.common.core.text.Convert;

/**
 * 测试项目审核流程Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-21
 */
@Service
public class GysptTestServiceImpl implements IGysptTestService 
{
    @Autowired
    private GysptTestMapper gysptTestMapper;
    
    @Resource
    private RuntimeService runtimeService;

    @Resource
    private TaskService taskService;

    @Resource
    IdentityService identityService;

    /**
     * 查询测试项目审核流程
     * 
     * @param id 测试项目审核流程主键
     * @return 测试项目审核流程
     */
    @Override
    public GysptTest selectGysptTestById(Long id)
    {
        return gysptTestMapper.selectGysptTestById(id);
    }

    /**
     * 查询测试项目审核流程列表
     * 
     * @param gysptTest 测试项目审核流程
     * @return 测试项目审核流程
     */
    @Override
    public List<GysptTest> selectGysptTestList(GysptTest gysptTest)
    {
        return gysptTestMapper.selectGysptTestList(gysptTest);
    }

    /**
     * 新增测试项目审核流程
     * 
     * @param gysptTest 测试项目审核流程
     * @return 结果
     */
    @Override
    public int insertGysptTest(GysptTest gysptTest)
    {
    	int rows = gysptTestMapper.insertGysptTest(gysptTest);
        // 发起请假流程
        identityService.setAuthenticatedUserId(gysptTest.getUserId());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gysptTest.getUserId());
        variables.put("level1", gysptTest.getLevel1());
        runtimeService.startProcessInstanceByKey("text", String.valueOf(gysptTest.getId()), variables);
        
        return rows;
    }

    /**
     * 修改测试项目审核流程
     * 
     * @param gysptTest 测试项目审核流程
     * @return 结果
     */
    @Override
    public int updateGysptTest(GysptTest gysptTest)
    {
        return gysptTestMapper.updateGysptTest(gysptTest);
    }

    /**
     * 批量删除测试项目审核流程
     * 
     * @param ids 需要删除的测试项目审核流程主键
     * @return 结果
     */
    @Override
    public int deleteGysptTestByIds(String ids)
    {
        return gysptTestMapper.deleteGysptTestByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除测试项目审核流程信息
     * 
     * @param id 测试项目审核流程主键
     * @return 结果
     */
    @Override
    public int deleteGysptTestById(Long id)
    {
        return gysptTestMapper.deleteGysptTestById(id);
    }
}
